package com.quanquan.user.controller;

import DTO.PageDTO;
import com.quanquan.user.entity.IntegralTradingDetails;
import com.quanquan.user.entity.otherCrm.bean.GetVipScoreItem;
import com.quanquan.user.entity.otherCrm.bean.VipScoreItem;
import com.quanquan.user.exception.IntegralException;
import com.quanquan.user.request.IntegralDetailsRequestBody;
import com.quanquan.user.service.IntegralTradingDetailsService;
import com.quanquan.user.vo.IntegralTradingDetailsVO;
import commond.IntegralOutPut;
import enums.ResCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.DateUtils;
import web.response.ResultBody;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能描述:积分交易管理 @ClassName: IntegralDetailsController @Author: xiaoqianh丶 @Date: 2019-03-21
 * 15:55 @Version: V1.0
 */
@Controller
@RequestMapping("/integral_details")
public class IntegralTradingDetailsController {

    private final IntegralTradingDetailsService integralTradingDetailsService;


    @Autowired
    public IntegralTradingDetailsController(
            IntegralTradingDetailsService integralTradingDetailsService) {
        this.integralTradingDetailsService = integralTradingDetailsService;
    }

    @PostMapping("/record")
    @ResponseBody
    public ResultBody addRecord(@RequestBody IntegralDetailsRequestBody requestBody) {
        try {
            integralTradingDetailsService.createIntegralDetails(requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }

    /**
     * 查看积分列表
     *
     * @param requestBody
     */
    @PostMapping("/query_integral_list")
    @ResponseBody
    private ResultBody getIntegralDetailsList(@RequestBody IntegralDetailsRequestBody requestBody) {
        PageDTO<IntegralTradingDetailsVO> pageDTO = null;
        try {
            pageDTO = integralTradingDetailsService.findIntegralTrading(requestBody);
        } catch (IntegralException e) {
            e.printStackTrace();
        }
        return new ResultBody(
                ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), pageDTO);
    }

    @PostMapping("/query_integral_user")
    @ResponseBody
    public ResultBody getIntegralDetails(@RequestBody IntegralDetailsRequestBody requestBody) {

        Integer page = null == requestBody.getPage() ? 0 : requestBody.getPage();
        Integer size = null == requestBody.getSize() ? 10 : requestBody.getSize();
        String userId = requestBody.getUserId();
        Integer tradeAction = requestBody.getTradeAction();
        String years = requestBody.getYears();

        List<IntegralTradingDetails> persionalIntegralTradingDetails =
                integralTradingDetailsService.findPersionalIntegralTradingDetails(userId, tradeAction, page, size,years);
        if (persionalIntegralTradingDetails == null) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), "没有相应的积分记录");
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), persionalIntegralTradingDetails);
    }


    @PostMapping("/select_integral_trading")
    @ResponseBody
    public ResultBody  queryIntegralTrading(@RequestBody  IntegralDetailsRequestBody requestBody){
        String years = requestBody.getYears();
        String userId = requestBody.getUserId();
        List<IntegralTradingDetails> yearsIntegralTrading = integralTradingDetailsService.findYearsIntegralTrading(userId, years);
        if( yearsIntegralTrading.size()<=0){
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),"没有数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), yearsIntegralTrading);
    }


}
